<?php
// 主页
namespace Home\Controller;

use Think\Controller;
use Home\Model\UserModel;
use Admin\Model\GuestbookModel;
use Home\Model\BlogModel;
use Home\Model\KeywordModel;
use Home\Model\CommentModel;
use Home\Model\SiteModel;
use Home\Model\ArticleModel;
use Home\Model\TougaoModel;

class AjaxController extends CommonController {
	public function user() {
		$do = $_REQUEST ['do'];
		$username = $_REQUEST ['username'];
		$password = $_REQUEST ['password'];
		$oldpassword = $_REQUEST ['oldpassword'];
		$newpassword = $_REQUEST ['newpassword'];
		$token = $_REQUEST ['token'];
		$email = $_REQUEST ['email'];
		$uid = intval ( $_REQUEST ['uid'] );
		$user = new UserModel ();
		switch ($do) {
			case 'register' :
				$rs = $user->register ( $username, $password, $email );
				break;
			case 'login' :
				$rs = $user->login ( $username, $password );
				break;
			case 'logout' :
				$rs = $user->logout ();
				break;
			case 'repassword' :
				$rs = $user->editPassword ( $newpassword, $oldpassword );
				break;
			case 'refindpassword' :

				$rs = check_token ( $uid, $token );
				if ($rs ['status']) {
					$rs = $user->reFindPassword ( $uid, $password );
				} else {
					$rs ['status'] = 0;
					$rs ['msg'] = 'token验证失败';
				}
				break;
			case 'findpassword' :
				$rs = $user->findPassword ( $email );
				break;
			case 'sendActivation' :
				$rs = $user->sendActivation ();
				break;
			default :
				$rs = 0;
		}
		$this->ajaxReturn ( $rs );
	}
	public function guestbook() {
		$guestbook = new GuestbookModel ();
		$rs = $guestbook->saveData ( $_POST );
		$this->ajaxReturn ( $rs );
	}
	public function blog() {
		$do = $_REQUEST ['do'];
		$uid = intval ( $_REQUEST ['uid'] );
		$blog = new BlogModel ();
		switch ($do) {
			case 'add' :
				$rs = $blog->saveData ( $_POST );
				break;
			case 'getUserBlog' :
				$rs = $blog->getUserBlog ( $uid );
				break;
		}
		$this->ajaxReturn ( $rs );
	}
	public function keyword() {
		$key = $_REQUEST ['keyword'];
		$keyword = new KeywordModel ();
		$rs = $keyword->getKeyWork ( $key );
		$this->ajaxReturn ( $rs );
	}
	public function comment() {
		$comment = new CommentModel ();
		$do = $_REQUEST ['do'];
		switch ($do) {
			case 'add' :
				$rs = $comment->saveData ( $_POST );
				break;
			case 'list' :
				$obj_id = intval ( $_REQUEST ['obj_id'] );
				$obj_type = $_REQUEST ['obj_type'];
				$rs = $comment->getComment ( $obj_id, $obj_type );
				break;
			case 'getUserComment' :
				$rs = $comment->getUserComment ();
				break;
			default :
				$rs = 0;
		}
		$this->ajaxReturn ( $rs );
	}
	public function site() {
		$site = new SiteModel ();
		$id = intval ( $_REQUEST ['id'] );
		$do = $_REQUEST ['do'];
		switch ($do) {
			case 'addview' :
				$result = $site->where ( 'id=' . $id )->setInc ( 'viewnum', 1 );
				if ($result) {
					$rs ['status'] = 1;
					$rs ['msg'] = '增加成功';
				} else {
					$rs ['status'] = 0;
					$rs ['msg'] = '增加失败';
				}
				break;
			default :
				$rs = 0;
		}
		$this->ajaxReturn ( $rs );
	}
	public function article() {
		$article = new ArticleModel ();
		$id = intval ( $_REQUEST ['id'] );
		$do = $_REQUEST ['do'];
		switch ($do) {
			case 'addview' :
				$result = $article->where ( 'id=' . $id )->setInc ( 'viewnum', 1 );
				if ($result) {
					$rs ['status'] = 1;
					$rs ['msg'] = '增加成功';
				} else {
					$rs ['status'] = 0;
					$rs ['msg'] = '增加失败';
				}
				break;
			default :
				$rs = 0;
		}
		$this->ajaxReturn ( $rs );
	}
	public function tougao() {
		$tougao = new TougaoModel ();
		$id = intval ( $_REQUEST ['id'] );
		$url = $_REQUEST ['url'];
		$do = $_REQUEST ['do'];
		switch ($do) {
			case 'checkurl' :
				$result = $tougao->checkUrl ( $url, $id );
				if ($result) {
					$rs ['status'] = 0;
					$rs ['msg'] = 'url已经存在';
				} else {
					$rs ['status'] = 1;
					$rs ['msg'] = 'url可以使用';
				}
				break;
			default :
				$rs = 0;
		}
		$this->ajaxReturn ( $rs );
	}
}
